package com.xand.mtgox.connector;

import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIOException;

import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xand.bt.api.MarketApiConst;
import com.xand.bt.api.MarketImageChangeNotify;

public class MtGoxIOCallback implements IOCallback {
	
	private static final Logger logger = LoggerFactory.getLogger(MtGoxIOCallback.class);
	
	private TemporaryMarketImageChangesStore tmpMarketImageChangesStore;
	
	public MtGoxIOCallback() {

		this.tmpMarketImageChangesStore = new TemporaryMarketImageChangesStore();
		
	}
	
	public TemporaryMarketImageChangesStore getTemporaryMarketImageChangesStore() {
		
		return this.tmpMarketImageChangesStore;
		
	}
	
	public void on(String event, IOAcknowledge ioAck, Object... args) {
		
		logger.info("on");
		
	}

	public void onConnect() {
		
		logger.info("Connect event received.");
		
	}

	public void onDisconnect() {
		
		logger.info("onDisconnect");
		
	}

	public void onError(SocketIOException exception) {
		
		logger.error("error", exception);
		
	}

	public void onMessage(String data, IOAcknowledge ioAck) {
		
		logger.info("onMessage1");
		
	}

	public void onMessage(JSONObject json, IOAcknowledge ioAck) {
		
		synchronized(SyncLock.LOCK) {
		
			String channel = null;
			
			try {
				channel = json.getString("channel");
			}catch(Exception e){
				e.printStackTrace();
			}
			
			if("24e67e0d-1cad-4cc0-9e7a-f8523ef460fe".equals(channel)) {
				
				try {
					
					if(json.has("depth")) {
						
						JSONObject depth = json.getJSONObject("depth");
						
						String sCurrency = depth.getString("currency");
						String sItem = depth.getString("item");
						
						String sPrice = depth.getString("price_int");
						String sType = depth.getString("type_str");
						String sVolume = depth.getString("volume_int");
						String sNow = depth.getString("now");
	
						MarketImageChangeNotify change = new MarketImageChangeNotify();
						
						if("ask".equals(sType)) {
							change.setDirection(MarketApiConst.DIRECTION_ASK);
						}else {
							change.setDirection(MarketApiConst.DIRECTION_BID);
						}
						
						long price = Long.parseLong(sPrice);
						change.setPrice(price);
						
						long volume = Long.parseLong(sVolume);
						change.setVolume(volume);
						
						long now = Long.parseLong(sNow);
						change.setTime(now / 1000L);
						
						change.setMarketLabel(MtGoxConnector.MARKET_LABEL);
						
						if(price != 0 || volume != 0) {
							
							MtGoxMarketChangesStore store = MtGoxMarketChangesStore.getInstance();
							
							if(store.isSynchronizationDone()) {
								
								store.addMarketChangeNotify(change, false, true);
								
							}else{
								
								this.tmpMarketImageChangesStore.addMarketImageChange(change);
								
							}
							
						}
						
					}
					
				}catch(Exception e){
					logger.error("An error occured", e);
					throw new RuntimeException(e);
				}
			}
		}
	}
}
